package com.tucc.hello.leetcode.other;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
//        String s = "abcd", t = "bcdf";
        String s = "abcd", t = "cdef";
        // 不超过 cost的前提下， 将s变换成t , 变换完之后最长的相同子串的长度
        int cost = 6;
        System.out.println(solution.equalSubstring(s, t, cost));
    }

    public int equalSubstring(String s, String t, int maxCost) {
        int max = 0;
        int left = 0;
        int curUse = 0;
        for (int right = 0; right < s.length(); right++) {
            int cost = Math.abs(s.charAt(right) - t.charAt(right));
            curUse += cost;
            while (curUse > maxCost) {
                curUse -= Math.abs(s.charAt(left) - t.charAt(left));
                left++;
            }
            max = Math.max(max, right - left + 1);
        }
        return max;
    }

}
